package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.exception.AddressBookBusinessException;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import com.sun.corba.se.impl.protocol.AddressingDispositionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 地址服务层接口实现类
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 通过当前用户获取当前用户的地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
        //实例化地址信息
        AddressBook addressBook = new AddressBook();
        //设置地址用户的ID
        addressBook.setUserId(BaseContext.getCurrentId());
        //通过用户ID获取地址相关信息
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        //返回地址信息
        return addressBookList;
    }

    /**
     * 获取当前用户的默认地址
     * @return
     */
    @Override
    public AddressBook defaultAddress() {
        //实例化地址对象
        AddressBook addressBook = new AddressBook();
        //设置用户ID到地址中
        addressBook.setUserId(BaseContext.getCurrentId());
        //通过用户ID获取此用户的所有地址
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        //判断是否拥有地址
        if (addressBookList.size() > 0) {
            //遍历数据
            for (AddressBook book : addressBookList) {
                //判断当前的地址信息是否是默认地址
                if (book.getIsDefault().equals(StatusConstant.ENABLE)){
                    //返回默认地址
                    return book;
                }
            }
        }
        //没有默认地址则返回空
        return null;
    }

    /**
     * 通过地址ID获取地址
     * @param id
     * @return
     */
    @Override
    public AddressBook getAddressBookById(Long id) {
        //通过地址ID获取地址信息
        AddressBook addressBook = addressBookMapper.getAddressBookById(id);
        //返回地址信息
        return addressBook;
    }

    /**
     * 新增地址信息
     * @param addressBook
     */
    @Override
    public void saveAddress(AddressBook addressBook) {
        //添加用户ID
        addressBook.setUserId(BaseContext.getCurrentId());
        //添加地址默认
        //如果之前没有地址则设置地址为默认地址
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        //查询当前地址是否为空
        if (addressBookList != null && addressBookList.size() > 0) {
            //不为空则禁用默认地址
            addressBook.setIsDefault(StatusConstant.DISABLE);
        }else {
            //为空则启用默认地址
            addressBook.setIsDefault(StatusConstant.ENABLE);
        }
        //新增地址信息
        addressBookMapper.saveAddress(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    @Transactional
    public void setDefault(AddressBook addressBook) {
        //首先把之前的默认地址改成 O
        //添加用户ID
        addressBook.setUserId(BaseContext.getCurrentId());
        //设置变量判断
        int flag = 0;
        //通过当前用户获取此用户的地址信息
        List<AddressBook> addressBookList = addressBookMapper.list(addressBook);
        //判断此用户的地址是否为空
        if (addressBookList != null && addressBookList.size() > 0) {
            //循环遍历地址信息
            for (AddressBook book : addressBookList) {
                //如果默认地址是启动状态
                if (book.getIsDefault().equals(StatusConstant.ENABLE)){
                    //设置默认地址的状态为禁用
                    book.setIsDefault(StatusConstant.DISABLE);
                    //更新地址信息
                    addressBookMapper.update(book);
                    //返回状态码 code 为 1
                    flag = 1;
                    break;
                }
            }
        }
        //更新新的默认地址
        if (flag == 1) {
            //更新新的地址为启用
            addressBook.setIsDefault(StatusConstant.ENABLE);
            //更新默认地址
            addressBookMapper.update(addressBook);
        }
    }

    /**
     * 更新地址信息
     * @param addressBook
     */
    @Override
    public void updateAddress(AddressBook addressBook) {
        //设置用户ID
        addressBook.setUserId(BaseContext.getCurrentId());
        //更新地址信息
        addressBookMapper.update(addressBook);
    }

    /**
     * 根据ID删除地址信息
     * @param id
     */
    @Override
    public void deleteAddressById(Long id) {
        //判断地址是否是默认地址 是 不可删除
        AddressBook addressBook = addressBookMapper.getAddressBookById(id);
        //获取此ID的地址信息
        if (addressBook != null){
            //判断此地址是否是默认地址
            if (addressBook.getIsDefault().equals(StatusConstant.ENABLE)){
                //是则删除地址
                addressBookMapper.deleteAddressById(id);
                //实例化新的地址
                AddressBook address = new AddressBook();
                //设置用户ID
                address.setUserId(BaseContext.getCurrentId());
                //通过用户ID获取当前用户的地址信息
                List<AddressBook> addressBookList = addressBookMapper.list(address);
                //判断地址是否为空
                if (addressBookList != null && addressBookList.size() > 0) {
                    //不为空则循环遍历
                    for (AddressBook book : addressBookList) {
                        //设置当前的地址为默认地址
                        book.setIsDefault(StatusConstant.ENABLE);
                        //更新数据
                        addressBookMapper.update(book);
                        //跳出循环
                        break;
                    }
                }
            }else{
                //否 删除
                addressBookMapper.deleteAddressById(id);
            }
        }
    }
}
